package com.soft.base.input;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.soft.base.util.Base64FileUtil;
import com.soft.base.util.Base64FileUtil;
import com.xiaoleilu.hutool.lang.Validator;
import com.xiaoleilu.hutool.util.ReUtil;
import com.xiaoleilu.hutool.util.StrUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
public class InputPackage {

    private Map<String, Object> data = new HashMap<>(1);

    private final String regix = "1\\d{10}";
    public static InputPackage build(Map<String, Object> body) {
        InputPackage input = new InputPackage();
        input.data = body;
        return input;
    }

    public String getTrimString(String name) {
        Object v = data.get(name);
        if (null != v) {
            if (v instanceof String) {
                return ((String) v).trim();
            }
        }
        return null;
    }

    public String getPhone(String name) {
        String v = getTrimString(name);
        if (ReUtil.isMatch(regix, v)) {
            return v;
        }
        return null;
    }

    public String getEmail(String name) {
        String v = getTrimString(name);
        if (Validator.isEmail(v)) {
            return v;
        }
        return null;
    }

    public Integer getInteger(String name) {
        Object v = data.get(name);
        if (null != v) {
            if (v instanceof Integer) {
                return (Integer) v;
            } else if (v instanceof Long) {
                return ((Long) v).intValue();
            }
        }
        return 0;
    }

    public Long getLong(String name) {
        Object v = data.get(name);
        if (null != v) {
            if (v instanceof Long) {
                return (Long) v;
            } else if (v instanceof Integer) {
                return ((Integer) v).longValue();
            }
        }
        return 0L;
    }

    public Base64File getBase64File(String name) {
        String file = getTrimString(name);
        if (StrUtil.isEmpty(file)) {
            return null;
        }
        for (Map.Entry<String, String> f : Base64FileUtil.listMimes().entrySet()) {
            String prefix = StrUtil.format("data:{};base64,", f.getValue());
            if (file.startsWith(prefix)) {
                Base64File base64File = new Base64File();
                base64File.setMime(f.getValue());
                base64File.setType(f.getKey());
                base64File.setData(file.substring(prefix.length()));
                return base64File;
            }
        }
        return null;
    }

    public Object getObject(String name) {
        return data.get(name);
    }

    public JSONObject getJsonObject(String name) {
        String file = getTrimString(name);
        if (StrUtil.isEmpty(file)) {
            return null;
        }
        return JSON.parseObject(file);
    }

    public List<Long> getStringSeparatedArrayLong(String name) {
        char separated = ',';
        String str = getTrimString(name);
        if (StrUtil.isEmpty(str)) {
            return new ArrayList<>();
        }
        ArrayList<Long> values = new ArrayList<>();
        for (String s : StrUtil.split(str, separated)) {
            values.add(Long.valueOf(s));
        }
        return values;
    }

    public List<String> getTrimStringArray(String name) {
        ArrayList<String> result = new ArrayList<>();
        Object o = data.get(name);
        if (o instanceof Iterable) {
            for (Object i : (Iterable) o) {
                if (i instanceof String) {
                    result.add(StrUtil.trim(((String) i)));
                }
            }
        }
        return result;
    }

    public List<Long> getLongArray(String name) {
        ArrayList<Long> result = new ArrayList<>();
        Object o = data.get(name);
        if (o instanceof Iterable) {
            for (Object i : (Iterable) o) {
                if (i instanceof Integer) {
                    result.add(((Integer) i).longValue());
                } else if (i instanceof Long) {
                    result.add(((Long) i));
                }
            }
        }
        return result;
    }
}
